#include "adc_config.h"

void my_adc_init(void)
{
    // ADC分频系数配置 72/6=12M
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);
    // 开启时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    // ADC1配置
    ADC_InitTypeDef ADC_InitStructure;
    ADC_InitStructure.ADC_DataAlign          = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStructure.ADC_ScanConvMode       = DISABLE;
    // 软件触发
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    ADC_InitStructure.ADC_Mode             = ADC_Mode_Independent;
    ADC_InitStructure.ADC_NbrOfChannel     = ADC_Channel_0;
    ADC_Init(ADC1, &ADC_InitStructure);
    // 开启ADC
    ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_13Cycles5);
    ADC_ExternalTrigConvCmd(ADC1, ENABLE);
    //闭合ADC总开关
    ADC_Cmd(ADC1, ENABLE);

    //配置ADC EOC中断
    ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
    //ADC中断优先级配置
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}